/**
 * 
 */
({
	single:true,
	launch : function() {
		var me = this;
		$.post(sys.ctx + "/user/setting/value", {field_name:"background"}, function(setting) {
			var currentBg = "";
			var currentType = "1";
			if(setting != "") {
				var bg = setting.split("`")
				currentBg = bg[0];
				currentType = bg[1];
			}
			var source = '<div><div class="easyui-layout" data-options="fit:true">';
			source += '<div data-options="region:\'west\'" style="width:150px;"></div>';
			source += '<div data-options="region:\'center\'" style="padding:5px;background:#eee;"></div>';
			source += "</div></div>";
			var footerSource = '<div align="right" style="padding:5px 5px;">';
			footerSource += '<select class="easyui-combobox">';
			footerSource += '<option value="1">拉伸</option>';
			footerSource += '<option value="2">平铺</option>';
			footerSource += '<option value="3">居中</option>';
			footerSource += '</select>&nbsp;';
			footerSource += '<a href="#">保存桌面</a>';
			footerSource += '</div>';
			var footer = $(footerSource);
			var selector = footer.children("select:first");
			selector.combobox();
			var save = footer.children("a:first");
			save.linkbutton({
				iconCls: 'icon-save'
			});
			var win = me.createWindow(source, {
				title:"桌面设置",
				footer : footer,
				width:830,
				height:460
			});
			var layout = win.children("div:first");
			var items = layout.children("div");
			layout.layout();
			var groups = {
				"system":"系统桌面",
				"mine":"我的桌面"
			};
			var view = items.eq(1);
			var selectIndex = 0;
			var bgs = $("<ul></ul>").appendTo(items.eq(0));
			bgs.datalist({
			    url: me.ctx + '/manager/backgrounds',
			    lines: true,
			    groupField:"group",
			    fit:true,
			    fixed:true,
			    groupFormatter:function(value, rows) {
			    	return groups[value];
			    },
			    textFormatter:function(value, row, index) {
			    	if(value) {
				    	if(value == currentBg) {
				    		selectIndex = index;
				    		selector.combobox('setValue', currentType);
				    	}
				    	return '<img src="'+me.ctx + value +'" width="100%" />';
			    	}
			    	return "";
			    },
			    onSelect:function(index,row) {
			    	view.css({
						"background-image" : "url(" + sys.ctx + ""+row.text+")",
						"background-position" : "",
						"filter" : "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='scale')",  
						"-moz-background-size" : "100% 100%",  
			    		"background-size" : "100% 100%"
					});
			    },
			    onLoadSuccess:function() {
			    	bgs.datalist("selectRow", selectIndex);
			    }
			});
			save.click(function() {
				$.messager.mask().text("正在保存设置，请稍后……");
				var row = bgs.datalist("getSelected");
				var value = row.text + "`" + selector.combobox("getValue");
				$.post(me.ctx + "/user/setting/set", 
					{
						field_name:"background", value:value
					}, 
					function(data) {
						if(data.success) {
							me.setBackgroundBySetting(data.message.value);
						}
						$.messager.mask("close")
					}, "json");
				return false;
			});
		}, "text");
	}
})